<template>
  <div>
    <el-button size="small" @click="courseVisible = true">选择课程</el-button>
    <el-row v-if="checkedCourseId" type="flex" align="middle" class="file-item file-preview">
      <!--<div class="picture">-->
      <!--  <el-image :src="checkedCourse.cover_url">-->
      <!--    <div slot="error" class="image-slot"><i class="el-icon-picture-outline" /></div>-->
      <!--  </el-image>-->
      <!--</div>-->
      <!--<el-row type="flex" justify="space-between" class="info">-->
      <!--  <el-tooltip effect="dark" :content="checkedCourse.title" placement="bottom">-->
      <!--    <div class="name">{{ checkedCourse.title }}</div>-->
      <!--  </el-tooltip>-->
      <!--</el-row>-->

      <div class="advert-item__course">
        <div class="course-del">
          <span class="text">已选课程：{{ checkedCourse.title }}</span>
          <!--<i class="el-icon-close close"></i>-->
        </div>
      </div>

    </el-row>
    <el-dialog title="选择课程" :visible.sync="courseVisible" width="60%" top="7.5vh">
      <div class="choose-course">
        <el-row type="flex" align="middle" justify="start" class="audio-header">
          <el-cascader v-model="category" :options="options" :props="props" clearable size="small" @change="loadData" />
          <el-input v-model="searchTitle" class="input" placeholder="请输入课程名称" size="small" @change="loadData" />
        </el-row>
        <el-row type="flex" class="audio-main">
          <div class="left">
            <div class="title">课程类型</div>
            <div class="material-tree">
              <div v-for="node in types" :key="node.id">
                <el-row type="flex" align="middle" justify="space-between" class="custom-tree-node__content">
                  <el-row
                    type="flex"
                    class="item-box"
                    align="middle"
                    justify="space-between"
                    @click.native="selectCourseType(node.id)"
                  >
                    <i class="el-icon-folder-opened" />
                    <div class="text">{{ node.title }}</div>
                  </el-row>
                </el-row>
              </div>
            </div>
          </div>
          <div class="right">
            <el-scrollbar>
              <div class="right-main">
                <el-row :gutter="20">
                  <el-col v-if="!datas || datas.length == 0" :span="24">暂无课程</el-col>
                  <el-radio-group v-else v-model="checked" class="list-box">
                    <el-col v-for="(item, index) in datas" :key="index" :span="12">
                      <el-radio class="content" :label="item.id">
                        <div class="tag">
                          <i class="Angle" />
                          <i class="el-icon-check" />
                        </div>
                        <el-row type="flex" class="file-item">
                          <div class="picture">
                            <el-image :src="item.cover_url">
                              <div slot="error" class="image-slot"><i class="el-icon-document" /></div>
                            </el-image>
                          </div>
                          <el-row type="flex" justify="space-between" class="info">
                            <el-tooltip effect="dark" :content="item.title" placement="bottom">
                              <div class="name">{{ item.title }}</div>
                            </el-tooltip>
                            <el-row v-if="item.scribing_price == 0 && item.price == 0" type="flex">
                              <div class="free-price">免费</div>
                            </el-row>
                            <el-row v-else type="flex">
                              <div v-if="item.scribing_price > 0" class="scribing-price">
                                {{ item.scribing_price | moneySymbol }}
                              </div>
                              <div v-if="item.price != 0" class="price">{{ item.price | moneySymbol }}</div>
                              <div v-else class="free-price">免费</div>
                            </el-row>
                          </el-row>
                        </el-row>
                      </el-radio>
                    </el-col>
                  </el-radio-group>
                </el-row>
              </div>
            </el-scrollbar>
            <div>
              <el-pagination
                :current-page.sync="paginate.page"
                :page-size="paginate.count"
                layout="total, prev, pager, next"
                :total="total"
                @current-change="handleCurrentChange"
              />
            </div>
          </div>
        </el-row>
      </div>
      <div slot="footer" class="choose-footer">
        <el-button size="small" @click="courseVisible = false">取消</el-button>
        <el-button type="primary" size="small" @click="setSelect">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Number,
      default: 0
    },
    field: {
      type: String,
      required: true,
      default: ''
    },
    formDatas: {
      type: Object,
      required: true,
      default: _ => {
        return {};
      }
    }
  },
  data() {
    return {
      response: null,
      paginate: {
        count: 20,
        page: 1
      },
      category: [0],
      options: [],
      searchTitle: '',
      currentType: 0,
      courseVisible: false,
      checked: this.value,
      checkedCourseId: this.value,
      checkedCourse: this.$util.getDefValue(this.formDatas[this.field + '_attach'], null) || {}
    };
  },
  computed: {
    types() {
      if (this.response) {
        return this.response.data.types;
      }
      return [];
    },
    datas() {
      if (this.response) {
        return this.response.data.datas;
      }
      return [];
    },
    total() {
      if (this.response) {
        return this.response.data.paginate.total;
      }
      return 0;
    }
  },
  watch: {
    value: {
      handler(checked, old) {
        this.checked = checked;
        if (this.checkedCourseId != this.checked) {
          const attach = this.datas.find(item => item.id == checked);
          this.checkedCourse = attach;
        }
        this.checkedCourseId = this.checked;
        this.courseVisible = false;
      }
    },
    'formDatas.mhm_id': {
      handler(mhmId, old) {
        this.loadData();
        this.$emit('update:value', 0);
      }
    }
  },
  created() {
    this.loadData();
    this.getCategoryCascader();
  },
  methods: {
    getCategoryCascader: function () {
      this.$http.get(
          '/course/admin/classes/' + 0 + '/choose/courseCategory/cascader',
          {},
          response => {
            this.options = response.data;
          },
          _ => {
          },
          false
      );
    },
    loadData() {
      const [category = 0] = [...this.category].reverse();
      this.$http.get(
          '/course/admin/coupon/courseCard/courseList',
          {
            ...this.paginate,
            title: this.searchTitle,
            type: this.currentType,
            category: category,
            mhm_id: this.formDatas.mhm_id,
            filter_free: 1
          },
          response => {
            this.response = response;
          },
          _ => {
          }
      );
    },
    selectCourseType: function (id) {
      this.currentType = id;
      this.paginate = {
        count: 20,
        page: 1
      };
      this.loadData();
    },
    handleCurrentChange: function (page) {
      this.paginate.page = page;
      this.loadData();
    },
    setSelect: function () {
      this.$emit('update:value', this.checked);
      this.courseVisible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.file-preview {
  //margin-top: 10px;

  .advert-item__course {
    //margin-top: 10px;

    .course-add {
      display: flex;
      align-items: center;

      .text {
        font-size: 14px;
        font-weight: 400;
        color: #303133;
        margin-right: 10px;
      }
    }

    .course-del {
      //margin-left: 66px;
      margin-top: 10px;
      display: flex;
      align-items: center;

      .text {
        flex: 1;
        min-width: 0;
        font-size: 12px;
        font-weight: 400;
        color: #909399;
        margin-right: 10px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .close {
        font-size: 12px;
        color: #8c8c8c;
        cursor: pointer;
      }
    }
  }
}

.choose-course {
  .input,
  .el-cascader {
    ::v-deep.el-input__inner {
      border-color: #dcdfe6;

      &:focus {
        border-color: #409eff;
      }
    }
  }
}

.list-box {
  width: 100%;
}

.file-item {
  //border: 1px solid #ececec;
  //padding: 5px;
  width: max-content;

  .picture {
    padding-top: 56px;
    width: 56px;
    height: 0;
    position: relative;
    flex-shrink: 0;

    .el-image {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;

      ::v-deep.image-slot {
        font-size: 36px;
      }
    }
  }

  .info {
    margin-left: 10px;
    flex: 1;
    flex-direction: column;

    .name {
      width: 100%;
      font-size: 14px;
      color: #333;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .desc {
      color: #909399;
      font-size: 12px;
    }

    .price {
      color: $price-color;
    }

    .scribing-price {
      text-decoration: line-through;
      color: $scribing-price-color;
      margin-right: 5px;
    }

    .free-price {
      color: $free-price-color;
    }
  }
}

::v-deep.el-form-item {
  margin-bottom: 22px !important;
}

.choose-course {
  .audio-header {
    .input {
      width: 220px;
      margin-left: 10px;
    }

    .link {
      margin-left: 10px;
    }
  }

  .audio-main {
    margin-top: 30px;

    .left,
    .right {
      border: 1px solid #e4e7ed;
    }

    .left {
      width: 244px;
      margin-right: 20px;

      .title {
        font-size: 16px;
        font-weight: 600;
        color: #303133;
        line-height: 50px;
        padding: 0 14px;
      }

      .material-tree {
        // height: calc(100vh - 275px);
        position: relative;

        .custom-tree-node__content {
          padding: 0 15px;
          height: 40px;

          .text {
            font-size: 13px;
            color: #606266;
          }

          .item-box {
            cursor: pointer;
          }
        }

        .custom-tree-node__children {
          .custom-tree-node {
            padding-left: 30px;
            padding-right: 15px;
            height: 40px;

            .item-box {
              flex: 1;
              cursor: pointer;
            }

            .text {
              flex: 1;
              height: 40px;
              line-height: 40px;
              font-size: 13px;
              color: #606266;
            }

            .el-icon-circle-plus-outline {
              font-size: 18px;
              color: $text-color-primary;
              cursor: pointer;
            }
          }
        }

        .el-icon-folder-opened {
          font-size: 16px;
          color: $text-color-primary;
          margin-right: 6px;
        }
      }
    }

    .right {
      height: 370px;
      flex: 1;

      .right-main {
        padding: 20px 20px 0;
      }

      .content {
        display: block;
        margin-bottom: 15px;
        padding: 12px 10px;
        border: 1px solid #e4e7ed;
        position: relative;

        &.is-checked {
          border-color: $text-color-primary;
        }

        .file-item {
          border: none;
          width: auto;

          .info {
            .name {
              width: 180px;
            }
          }
        }

        .el-radio__input.is-checked + .el-radio__label .tag {
          opacity: 1;
        }

        .tag {
          position: absolute;
          right: 0;
          top: 0;
          z-index: 10;
          opacity: 0;

          .Angle {
            position: absolute;
            right: -13px;
            top: -13px;
            width: 0;
            height: 0;
            border: 13px solid transparent;
            border-top-color: $text-color-primary;
            transform: rotate(-135deg);
          }

          .el-icon-check {
            position: absolute;
            right: 0;
            top: -1px;
            color: #fff;
            font-size: 12px;
          }
        }

        ::v-deep.el-checkbox__input {
          position: absolute;
          opacity: 0;
          top: 0;
        }

        ::v-deep.el-checkbox__label {
          display: block;
          padding-left: 0;
          line-height: 1;
        }

        ::v-deep.el-radio__inner {
          position: absolute;
          opacity: 0;
          top: 0;
        }

        .el-checkbox__input.is-checked + .el-checkbox__label .tag {
          opacity: 1;
        }
      }
    }

    ::v-deep.el-scrollbar {
      height: 100%;

      .el-scrollbar__wrap {
        overflow-x: auto;
      }
    }
  }
}
</style>
